import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FamilyFinanceManager extends JFrame {
    private JButton incomeButton;
    private JButton expenseButton;
    private JButton budgetButton;
    private JButton analysisButton;
    private JButton dataButton;

    public FamilyFinanceManager() {
        setTitle("家庭财务管理器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);

        // 设置标题栏
        setJMenuBar(createMenuBar());

        // 中间区域按钮布局
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2, 2, 10, 10));
        incomeButton = new JButton("收入管理");
        expenseButton = new JButton("支出管理");
        budgetButton = new JButton("预算管理");
        analysisButton = new JButton("统计分析");
        dataButton = new JButton("数据管理");

        buttonPanel.add(incomeButton);
        buttonPanel.add(expenseButton);
        buttonPanel.add(budgetButton);
        buttonPanel.add(analysisButton);
        buttonPanel.add(dataButton);

        add(buttonPanel, BorderLayout.CENTER);

        // 添加按钮监听器
        addListeners();

        // 显示主窗口
        setVisible(true); // 确保主窗口可见
    }

    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        return menuBar;
    }

    private void addListeners() {
        incomeButton.addActionListener(e -> showIncomeManagement());
        expenseButton.addActionListener(e -> showExpenseManagement());
        budgetButton.addActionListener(e -> showBudgetManagement());
        analysisButton.addActionListener(e -> showAnalysis());
        dataButton.addActionListener(e -> showData());
    }

    private void showIncomeManagement() {
        // 跳转到收入管理界面
        IncomeManagement incomeManagement = new IncomeManagement(this);
        incomeManagement.setVisible(true);
        this.setVisible(false);
    }

    private void showExpenseManagement() {
        // 跳转到支出管理界面
        ExpenseManagement expenseManagement = new ExpenseManagement(this);
        expenseManagement.setVisible(true);
        this.setVisible(false);
    }

    private void showBudgetManagement() {
        // 跳转到预算设定界面
        BudgetManagement budgetManagement = new BudgetManagement(this);
        budgetManagement.setVisible(true);
        this.setVisible(false);
    }

    private void showAnalysis() {
        // 跳转到统计分析界面
        Analysis analysis = new Analysis(this);
        analysis.setVisible(true);
        this.setVisible(false);
    }

    private void showData() {
        // 跳转到数据管理界面
        DataManagement dataManagement = new DataManagement(this);
        dataManagement.setVisible(true);
        this.setVisible(false);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                new FamilyFinanceManager(); // 创建并显示主窗口
            } catch (Exception e) {
                e.printStackTrace(); // 捕获并打印任何异常
            }
        });
    }
}